package com.tuton.javanet.chapter03;

public class Thread02ThreadPoolTester {

	public static void main(String[] args) {
		if(args.length != 2) {
			System.out.println("Way to run : java ThreadPlooTester poolSize taskNumber");
			System.out.println("poolSize : the size of the ThreadPool" );
			System.out.println("taskNumber : the number of the tasks");
			
			return;
		}
		
		int poolSize = Integer.parseInt(args[0]);
		int taskNumber = Integer.parseInt(args[1]);
		
		Thread02ThreadPool threadPool = new Thread02ThreadPool(poolSize);
		
		for(int i = 0; i < taskNumber; i++) {
			Runnable task = createTask(i);
			threadPool.execute(task);
		}
		
		threadPool.join();

	}

	private static Runnable createTask(final int taskID) {
		return new Runnable(){

			public void run() {
				System.out.println("task " + taskID + " was started..." );
				try{
					Thread.sleep(3000);
				} catch(InterruptedException ex) {
					ex.printStackTrace();
				}
				System.out.println("task " + taskID + " was ended..." );
				
			}
			
		};
	}

}
